%BESTRANKSEXAMPLE - Example for the use of bestRanks.
%bestRanksExample applies rank-optimal weighting to the OECD`s Better Life
%Index data using the function bestRanks.
%
% Other m-files required: bestRanks.m
% Other files required: oecd_betterlife_2013.csv,  oecd_betterlife_2014.csv
%
% See also: BESTRANKS

% Author: Christoph Brauer
% TU Braunschweig
% email: ch.brauer@tu-bs.de
% Website: https://www.tu-braunschweig.de/iaa/personal/brauer
% July 2015; Last revision: 22-July-2015

% read 2013 index data from raw data file
BLI = readtable('oecd_betterlife_2013.csv');
indicators = BLI(:,{'dim_housing','dim_income','dim_jobs','dim_community', 'dim_education','dim_environment','dim_civicengagement','dim_health','dim_lifesatisfaction','dim_safety','dim_worklifebalance'});
indicators = table2array(indicators);

% calculate best ranks using integer weights and second order distance
% maximization
[weights, ranks, distances] = bestRanks(indicators, 'int', 'distance');

% print formatted output
countries = BLI(:,{'country'});
countries = table2array(countries);
for k=1:length(countries)
    fprintf('Country: '), disp(countries(k)),
    fprintf('Best rank: %d\n\n',ranks(k))
    fprintf('Calculated Weights:\n')
    fprintf('Housing | Income | Jobs | Community | Education | Environment | Civic  Engagement | Health | Life Satisfaction | Safety | Work-Life Balance\n') 
    fprintf('%d       | %d      | %d    | %d         | %d         | %d           | %d                 | %d      | %d                 |%d       | %d\n\n',round(weights(k,:)))
end